import { request, useUserStore, clearEmpty } from "jmash-core";
import type { AxiosPromise } from "axios";
import type {
  ProcDefApi,
  ProcDefReq,
  FlowDeployReq,
  FlowDeployRes,
  FlowDeployXmlReq,
  FlowProcDefList,
  ProcDefKey,
  FlowProcDefModel,
  HiProcinstKey,
  HiProcInstModel,
} from "./types";

class ProcDefApiImpl implements ProcDefApi {
  getKey(model: FlowProcDefModel): ProcDefKey {
    return {
      procDefId: model.id,
    } as ProcDefKey;
  }

  //  部署流程
  deployFlow(query: FlowDeployReq): AxiosPromise<FlowDeployRes> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/flow/deploy/" + tenant,
      method: "post",
      data: query,
    });
  }

  //  部署流程
  deployFlowXml(query: FlowDeployXmlReq): AxiosPromise<FlowDeployRes> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/flow/deployxml/" + tenant,
      method: "post",
      data: query,
    });
  }

  // 删除
  deleteDeployment(deploymentId: string): AxiosPromise<boolean> {
    const tenant = useUserStore().user.tenant;
    return request({
      url: "/v1/flow/deploy/" + tenant + "/" + deploymentId,
      method: "delete",
    });
  }

  // 查询列表信息
  findList(query?: ProcDefReq): AxiosPromise<FlowProcDefList> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/flow/prodef/list/" + tenant,
      method: "get",
      params: query,
    });
  }

  // 查询
  findById(query: ProcDefKey): AxiosPromise<FlowProcDefModel> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/flow/prodef/id/" + tenant + "/" + query.procDefId,
      method: "get",
    });
  }

  // 通过ID查询流程定义XML
  findProcDefXmlById(procDefId: string): AxiosPromise<string> {
    const tenant = useUserStore().user.tenant;
    return request({
      url: "/v1/flow/prodef/xml/" + tenant + "/" + procDefId,
      method: "get",
    });
  }

  // 查询流程图
  findProcDefDiagram(procDefId: string): AxiosPromise {
    const tenant = useUserStore().user.tenant;
    return request({
      url: "/v1/flow/prodef/diagram/" + tenant + "/" + procDefId,
      method: "get",
      responseType: "blob",
    });
  }

  findHiProcinst(query: HiProcinstKey): AxiosPromise<HiProcInstModel> {
    const tenant = useUserStore().user.tenant;
    return request({
      url: "/v1/flow/hi_procinst/id/" + tenant + "/" + query.id,
      method: "get",
    });
  }
}
const procDefApi = new ProcDefApiImpl();

export { procDefApi };
